#!/usr/bin/python3
# -*- coding:utf-8 -*-
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer


def get_user(userfile):
    #定义一个用户列表
    user_list = []
    with open(userfile,encoding='utf-8') as f:
        for line in f:
            # print(len(line.split()))
            if not line.startswith('#') and line:
                if len(line.split()) == 4: 
                    user_list.append(line.split())
                else:
                    print("user.conf配置错误")
    return user_list

#实例化虚拟用户，这是FTP验证首要条件
authorizer = DummyAuthorizer()

#添加用户权限和路径，括号内的参数是(用户名， 密码， 用户目录， 权限)
#authorizer.add_user('user', '12345', '/home/', perm='elradfmw')
user_list = get_user('C:\\Users\\vainuer\\Desktop\\Test\\FtpServer\\user.conf')
for user in user_list:
    name, passwd, permit, homedir = user
    try:
        authorizer.add_user(name, passwd, homedir, perm=permit)
    except Exception as e:
        print(e)

#添加匿名用户 只需要路径
authorizer.add_anonymous('C:\\Users\\vainuer\\Desktop\\Test\\FtpServer')

#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer

#添加被动端口范围
handler.passive_ports = range(2000, 2333)

#监听ip 和 端口
server = FTPServer(('127.0.0.1', 2121), handler)

#开始服务
server.serve_forever()